/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.imageview.preview.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TableLayoutManager;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.ImageViewInfo;
import com.xuexiang.xuidemo.provider.XulVideoListProvider;
import com.xuexiang.xuidemo.refresh.IRefresh;
import com.xuexiang.xuidemo.refresh.SwipeRefreshLayout;
import com.xuexiang.xuidemo.refresh.TextOverComponent;

import java.util.ArrayList;
import java.util.List;

/**
 * RecycleView视频
 *
 * @since 2021-04-08
 */
public class VideoSlice extends AbilitySlice implements XulVideoListProvider.OnItemClickListener {
    private Text mSwitch;
    private Image mImageBack;
    private List<ImageViewInfo> videoImageList = new ArrayList<>();
    private XulVideoListProvider xulVideoListProvider;
    private SwipeRefreshLayout refresh;
    private HmEventHandler mEventHandler;
    private EventRunner runner;
    private Runnable task;
    private final int delayTime = 1000;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_preview_video_image);
        initView();
    }

    @Override
    protected void onBackground() {
        VideoSlice.this.onInactive();
    }

    private void initView() {
        mSwitch = (Text) findComponentById(ResourceTable.Id_tx_switch);
        mImageBack = (Image) findComponentById(ResourceTable.Id_image_back);
        mSwitch.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName(getBundleName())
                    .withAbilityName("com.xuexiang.xuidemo.slice.imageview.preview.PreviewRecycleViewAbility")
                    .build();
                intent.setOperation(operation);
                startAbility(intent);
                setTransitionAnimation(0, 0);
            }
        });
        mImageBack.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                present(new PreviewImageSlice(), new Intent());
            }
        });
        initData();
        ListContainer mLcImage = (ListContainer) findComponentById(ResourceTable.Id_lc_image);
        xulVideoListProvider = new XulVideoListProvider(videoImageList, this, this);
        TableLayoutManager tableLayoutManager = new TableLayoutManager();
        tableLayoutManager.setColumnCount(2);
        tableLayoutManager.setOrientation(Component.HORIZONTAL);
        mLcImage.setOrientation(Component.VERTICAL);
        mLcImage.setLayoutManager(tableLayoutManager);
        mLcImage.setItemProvider(xulVideoListProvider);
        initHandler();
        initRefresh();
        refresh.refresh();
    }

    private void initRefresh() {
        refresh = (SwipeRefreshLayout) findComponentById(ResourceTable.Id_refresh);

        // 创建下拉刷新头部组件
        TextOverComponent overView = new TextOverComponent(getContext());

        // 设置下拉刷新的头部组件
        refresh.setRefreshOverComponent(overView);
        refresh.setRefreshListener(new IRefresh.RefreshListener() {
            @Override
            public void onRefresh() {
                mEventHandler.postTask(task, delayTime, EventHandler.Priority.IMMEDIATE); // 延时250ms后立即处理
            }

            /**
             * 允许下拉刷新
             *
             * @return boolean
             */
            @Override
            public boolean enableRefresh() {
                return true;
            }
        });
    }

    private void initHandler() {
        runner = EventRunner.getMainEventRunner();
        task = new Runnable() {
            @Override
            public void run() {
                xulVideoListProvider.notifyDataChanged();
                refresh.refreshFinish();
            }
        };
        mEventHandler = new HmEventHandler(runner);
    }

    private void initData() {
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
            "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
            "https://pic.vjshi.com/2017-05-25/b146e104069c2bd0590bb919269193c4/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
            "https://pic.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53.mp4",
            "https://pic.vjshi.com/2017-05-07/d0bbfc4ac4dd173cc93873ed4eb0be53/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));

        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-07-18/80d08ce1a84adfbaed5c7067b73d19ed.mp4",
            "https://pic.vjshi.com/2017-07-18/80d08ce1a84adfbaed5c7067b73d19ed/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
            "https://pic.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
            "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
            "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2018-06-07/cf673556cce54ab9cf4633fd7d9d0d46.mp4",
            "https://pic.vjshi.com/2018-06-06/caa296729c8e6e41e6aff2aadf4feff3/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
            "https://pic.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a.mp4",
            "https://pic.vjshi.com/2017-09-13/f55a900d89679ac1c9837d5b5aaf632a/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2018-06-07/cf673556cce54ab9cf4633fd7d9d0d46.mp4",
            "https://pic.vjshi.com/2018-06-06/caa296729c8e6e41e6aff2aadf4feff3/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424.mp4",
            "https://pic.vjshi.com/2018-01-27/5169bb7bdd7386ce7bd4ce1739229424/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
            "https://pic.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));
        videoImageList.add(new ImageViewInfo("https://lmp4.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82.mp4",
            "https://pic.vjshi.com/2017-09-27/9a6e69f7c257ff7b7832e8bac6fddf82/online/puzzle.jpg?"
                + "x-oss-process=style/resize_w_285_crop_h_428"));

    }

    @Override
    public void onItemClick(Component component, int position) {
        int mPosition = position + 1;
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withDeviceId("")
            .withBundleName(getBundleName())
            .withAbilityName("com.xuexiang.xuidemo.slice.imageview.preview.BannerVideoPagerAbility")
            .build();
        intent.setParam("position", mPosition + "");
        intent.setOperation(operation);
        startAbility(intent);
        setTransitionAnimation(0, 0);
    }

    /**
     * HmEventHandler
     *
     * @since 2021-04-08
     */
    public static class HmEventHandler extends EventHandler {
        private HmEventHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            int id = event.eventId;
            long param = event.param;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        terminate();
    }
}
